// pages/posts/posts.js
var util = require('../../utils/util.js')
var greplyText= ''
var greplyId=0
var flagid=0
var gavatar=""
var gauthor=""
var gdate=""

Page({
    data: {
        viewdataTitle: '',
        viewdataText: '',
        viewdataAvatar: '',
        viewdataAuthor: '',
        viewdataDate: '',
        List:[],
        reply: []
    },

    onLoad(options) {
        flagid=options.id
        console.log(options.id)
        this.data.List=getApp().globalData.postsdata//从全局帖子数据读入

        if(wx.getStorageSync('ReplyData').length!=0) {
            getApp().globalData.replydata=wx.getStorageSync('ReplyData')
        }
        //console.log("ReplyData",wx.getStorageSync('ReplyData'))
        console.log("getApp().globalData.replydata",getApp().globalData.replydata)
        console.log("加载前this.data.reply",this.data.reply)

        this.data.List.forEach(item => {//循环遍历帖子数据，找到与帖子id相同的帖子
            if (flagid == item.id) {
                this.setData({
                    viewdataAvatar: getApp().globalData.postsdata[options.id].avatar,
                    viewdataAuthor: getApp().globalData.postsdata[options.id].author,
                    viewdataDate: getApp().globalData.postsdata[options.id].date,
                    viewdataTitle: getApp().globalData.postsdata[options.id].title,
                    viewdataText: getApp().globalData.postsdata[options.id].text
                })
            }
        })

        getApp().globalData.replydata.forEach(item =>{//循环遍历回复数据，找到与回复flag相同的回复
            if(flagid == item.flag){
                this.data.reply.push({
                    avatar: getApp().globalData.replydata[item.id].avatar,
                    author: getApp().globalData.replydata[item.id].author,
                    date: getApp().globalData.replydata[item.id].date,
                    id: getApp().globalData.replydata[item.id].id,
                    flag: getApp().globalData.replydata[item.id].flag,
                    text: getApp().globalData.replydata[item.id].text
                })
            }
            
            var arr=this.data.reply
            this.setData({
                reply: arr
            })
        })

    },

    GetReplyText (text) {
        greplyText=text.detail.value
        this.setData({
            greplyText: text.detail.value
        })
    },

    reply (){
        gdate = util.formatTime(new Date())
        var ggreplyId=getApp().globalData.replydata[getApp().globalData.replydata.length-1].id + 1
        greplyId=ggreplyId

        getApp().globalData.replydata.push({
            avatar: getApp().globalData.user.avatarUrl,
            author: getApp().globalData.user.nickName,
            date: gdate,
            id: greplyId,
            flag: Number(flagid),
            text: greplyText
        })
        wx.setStorageSync('ReplyData',getApp().globalData.replydata)//将回复数据存入缓存
        console.log("回复时getApp().globalData.replydata",getApp().globalData.replydata)
        getApp().globalData.replydata.forEach(item =>{
            if(greplyId==item.id && flagid==item.flag){//找到最新输入的回复数据
                this.data.reply.push({
                    avatar: getApp().globalData.user.avatarUrl,
                    author: getApp().globalData.user.nickName,
                    date: gdate,
                    id: getApp().globalData.replydata[item.id].id,
                    flag: getApp().globalData.replydata[item.id].flag,
                    text: getApp().globalData.replydata[item.id].text
                })
            }
        })
        var arr=this.data.reply
        this.setData({
            reply: arr
        })
        console.log("加载后this.data.reply",this.data.reply)
    }

})